<?php

namespace App\Admin\Metrics;

use App\Admin\Repositories\MemberTask;
use App\Admin\Repositories\OrgDepartmentOwner;
use App\Admin\Repositories\OrgTeamOwner;
use App\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Tasks extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('任务看板');
        $this->height(400);
        $this->chartHeight(300);
        $this->chartLabels('处理中任务占比');//计算公式：处理中/(待处理+处理中+已暂停)*100%
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $list = $this->getData();
        $total_cnt = collect($list)->except(\App\Models\MemberTask::TASK_STATUS_COMPLETE)->pluck('cnt')->sum();
        $ing_task_cnt = isset($list[\App\Models\MemberTask::TASK_STATUS_ING]) ? $list[\App\Models\MemberTask::TASK_STATUS_ING]['cnt'] : 0;
        $ready_task_cnt = isset($list[\App\Models\MemberTask::TASK_STATUS_READY]) ? $list[\App\Models\MemberTask::TASK_STATUS_READY]['cnt'] : 0;
        $pause_task_cnt = isset($list[\App\Models\MemberTask::TASK_STATUS_PAUSE]) ? $list[\App\Models\MemberTask::TASK_STATUS_PAUSE]['cnt'] : 0;
        // 卡片内容
        $this->withContent($total_cnt);
        // 卡片底部
        $this->withFooter($ing_task_cnt, $ready_task_cnt, $pause_task_cnt);
        // 图表数据
        $this->withChart(empty($total_cnt) ? 0 : ceil($ing_task_cnt/$total_cnt*100));
    }

    public function getData(array $team_id_list = [])
    {
        if (app(AdminController::class)->isAdmin()){
            return app(MemberTask::class)->getTaskCountByStatusUseTeamIds();
        }

        $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
        if ($owner_department_list){
            $department_id_list = $owner_department_list->pluck('department_id')->toArray();
        }
        $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentIds(Auth::guard('admin')->id(), $department_id_list);
        if ($owner_team_list){
            $team_id_list = $owner_team_list->pluck('team_id')->unique()->toArray();
        }
        return app(MemberTask::class)->getTaskCountByStatusUseTeamIds($team_id_list);
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center" style="width: 15rem">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <span>总任务数</span><small>（待处理+处理中+已暂停）</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-3" style="padding-top: 0!important;">
    <div class="text-center">
        <p>处理中数</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <div class="text-center">
        <p>待处理数</p>
        <span class="font-lg-1">{$open}</span>
    </div>
    <div class="text-center">
        <p>已暂停数</p>
        <span class="font-lg-1">{$response}</span>
    </div>
</div>
HTML
        );
    }
}
